<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<%@include file="include-head.jsp" %>
<link rel="stylesheet" href="css/pagination.css">
<script type="text/javascript" src="jquery/jquery.pagination.js"></script>

<body>
<%@include file="include-nav.jsp" %>
<div class="container-fluid">
    <div class="row">
        <%@include file="include-sidebar.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form action="admin/get/page" method="post" class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input name="keyWord" class="form-control has-success" value="${param.keyWord}" type="text" placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button type="submit" class="btn btn-warning"><i class="glyphicon glyphicon-search"></i> 查询
                        </button>
                    </form>
                    <button type="button" class="btn btn-danger deleted-admin-ids" style="float:right;margin-left:10px;"><i
                            class=" glyphicon glyphicon-remove"></i> 删除
                    </button>
                    <button type="button" class="btn btn-primary" style="float:right;"
                            onclick="window.location.href='admin/to/add/page'"><i class="glyphicon glyphicon-plus"></i> 新增
                    </button>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                                <tr>
                                    <th width="30">#</th>
                                    <th width="30"><input type="checkbox" name="selectall"></th>
                                    <th>账号</th>
                                    <th>名称</th>
                                    <th>邮箱</th>
                                    <th width="100">操作</th>
                                </tr>
                            </thead>


                            <c:if test="${empty requestScope.pageInfo.list}">
                                <tr>
                                    <td colspan="6">抱歉没有查询到相关数据</td>
                                </tr>
                            </c:if>
                            <c:if test="${!empty requestScope.pageInfo.list}">
                                <c:forEach items="${requestScope.pageInfo.list}" var="admin" varStatus="myStatus">
                                    <tr>
                                        <td style="display: none"></td>
                                        <td>${myStatus.count}</td>
                                        <td><input type="checkbox" name="checkId" value="${admin.id}"></td>
                                        <td>${admin.loginAcct}</td>
                                        <td>${admin.userName}</td>
                                        <td>${admin.email}</td>
                                        <td>
                                            <button type="button" class="btn btn-success btn-xs" onclick="window.location.href='assign/to/assign/role/page?adminId=${admin.id}&pageNum=${requestScope.pageInfo.pageNum}&keyWord=${param.keyWord}'"><i
                                                    class=" glyphicon glyphicon-check"></i></button>
                                            <button type="button" class="btn btn-primary btn-xs" onclick="window.location.href='admin/to/edit/page/${admin.id}?pageNum=${requestScope.pageInfo.pageNum}&keyWord=${param.keyWord}'"><i
                                                    class=" glyphicon glyphicon-pencil"></i></button>
                                            <button type="button" value="${admin.id}" class="btn btn-danger btn-xs  deleted-admin"><i
                                                    class=" glyphicon glyphicon-remove"></i></button>
                                        </td>
                                    </tr>
                                </c:forEach>
                            </c:if>


                            </tbody>

                            <tfoot>
                                <tr>
                                    <td colspan="6" align="center">
                                        <div id="Pagination" class="pagination"><!-- 这里显示分页 --></div>
                                    </td>
                                </tr>

                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

</body>


//分页
<script>
    $(function () {



        //分页
        initPagination();
    })
    //通过id删除
    $(".deleted-admin").click(function () {
        var id = $(this).attr("value");
        var obj = $(this).parent().parent();
        layer.confirm('确定删除吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                type: "POST",
                url: "deleted/admin",
                data: "id="+id,
                success: function(msg){
                    if(msg.result == "SUCCESS"){
                        obj.remove();
                        layer.msg('删除成功', {icon: 1});
                    }else{
                        var msg = $.parseJSON(msg)
                        layer.msg(msg.message, {icon: 2});
                    }

                }
            });

        });

    })
    /*批量选中的效果*/
    $('input:checkbox[name="selectall"]').click(function(){
        if($(this).is(':checked')){
            $('input:checkbox').each(function(){
                $(this).prop("checked",true);
            });
        }else{
            $('input:checkbox').each(function(){
                $(this).prop("checked",false);
            });
        }
    });
    //多选删除
    $(".deleted-admin-ids").click(function () {
        var checkId=$("input[name='checkId']:checked");
        var result = checkIds(checkId);
        var results = JSON.stringify(result);
        layer.confirm('确定删除吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){

            $.ajax({
                type: "POST",
                url: "deleted/adminIds",
                data: results,
                contentType:"application/json;charset=utf-8",
                success: function(msg){
                    if(msg.result == "SUCCESS"){
                        checkId.parents("tr").remove();
                        layer.msg('删除成功', {icon: 1});
                    }else{
                        var msg = $.parseJSON(msg)
                        layer.msg(msg.message, {icon: 2});
                    }

                }
            });

        });
    })


    function checkIds(checkId) {
        var result = [];
        for (var i=0;i < checkId.length;i++){
            result.push(checkId[i].value);
        }
        return result;
    }
    function initPagination() {
        var totalRecord = "${requestScope.pageInfo.total}";
        var page_num = parseInt("${requestScope.pageInfo.pageNum - 1}");
        var page_size = parseInt("${requestScope.pageInfo.pageSize}");
        // 创建分页
        $("#Pagination").pagination(totalRecord, {
            num_edge_entries: 3, //边缘页数
            num_display_entries: 5, //主体页数
            callback: pageSelectCallback,
            items_per_page:page_size,
            current_page:page_num,  //当前页，pagination默认从零开始,PageHerper默认从1开始所以要减一
            prev_text:"上一页",
            next_text:"下一页"
        });
    }

    // pageIndex是pagination传给我们的那个从0开始的页码
    function pageSelectCallback(pageIndex,jquery) {
        // 根据pageIndex计算得到pageNum

        var pageNum = pageIndex + 1;


        var keyWord = "${param.keyWord}";

        window.location.href = "admin/get/page?pageNum="+pageNum+"&keyWord="+keyWord;

        // 由于每一个按钮都是超链接，所以在这个函数最后取消超链接的默认行为

        return false;
    }
</script>
</html>